/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fbsystem;

import bo.MenuItem;
import databaseManager.DbConn;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 47
 */
public class TopMenuManager {

    public TopMenuManager() {
    }
    public String msg = "";
    public static String menuHTML = "";
    public static List topMenuList = new ArrayList();

    public static String resetMenus() {
        TopMenuManager.menuHTML = "";
        TopMenuManager.topMenuList = new ArrayList();
        TopMenuManager.getAllMenus();
        return menuHTML;
    }

    public static String getAllMenus() {
        if (!menuHTML.equals("")) {
            return menuHTML;
        }
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            String sql;
            sql = "select * from mainmenu order by parent_id,order_code,menu_id";
            conn = DbConn.getNewDbconn();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);//获得菜单项目
            bo.MenuItem item = null;
            List mList = new ArrayList();
            Map menuMap = new HashMap();
            while (rs.next()) {//组织所有菜单列表
                item = new MenuItem();
                item.setMenuId(rs.getLong("menu_id"));
                item.setMenuName(rs.getString("menu_name"));
                item.setMenuURI(rs.getString("menu_uri"));
                item.setParentId(rs.getLong("parent_id"));
                item.setOrderCode(rs.getLong("order_code"));
                mList.add(item);
                if (item.getParentId() == -1) {
                    menuMap.put(item.getMenuId(), item);
                    topMenuList.add(item);
                }
            }

            Iterator itr = mList.iterator();
            while (itr.hasNext()) {//生成菜单结构
                item = null;
                item = (MenuItem) itr.next();
                if (item.getParentId() != -1) {
                    ((MenuItem) menuMap.get(item.getParentId())).getChildren().add(item);
                }
                System.out.println(item.getMenuName());
            }
            if (!topMenuList.isEmpty()) {//组织html
                itr = topMenuList.iterator();
                while (itr.hasNext()) {//生成html
                    item = null;
                    item = (bo.MenuItem) itr.next();
                    menuHTML += "<td style=\"border-left:none;border-right:none\">&nbsp;|&nbsp;</td>" + item.composeHTML();
                }
            }
            if (!menuHTML.equals("")) {
                menuHTML += "<td style=\"border-left:none;border-right:none\">&nbsp;|&nbsp;</td>";
            }

        } catch (Exception ex) {
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception rse) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception stmte) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception conne) {
                }
            }
        }
        return menuHTML;
    }

    public boolean addMenuItem(String parentid, String itemname, String itemuri, String ordercode) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        if (parentid != null && itemname != null && itemuri != null && ordercode != null) {
            try {
                parentid = new String(parentid.getBytes("ISO-8859-1"), "UTF-8");
                itemname = new String(itemname.getBytes("ISO-8859-1"), "UTF-8");
                itemuri = new String(itemuri.getBytes("ISO-8859-1"), "UTF-8");
                ordercode = new String(ordercode.getBytes("ISO-8859-1"), "UTF-8");
                String sql = "insert into mainmenu(menu_name,menu_uri,parent_id,order_code) values";
                sql += "('" + itemname + "','" + itemuri + "'," + parentid + "," + ordercode + ")";
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                stmt.execute(sql);
                TopMenuManager.resetMenus();
                this.msg = "添加成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public boolean modifyMenuItem(String itemid, String itemname, String itemuri, String ordercode) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = "";
        if (itemid != null && itemname != null && itemuri != null && ordercode != null) {
            try {
                itemid = new String(itemid.getBytes("ISO-8859-1"), "UTF-8");
                itemname = new String(itemname.getBytes("ISO-8859-1"), "UTF-8");
                itemuri = new String(itemuri.getBytes("ISO-8859-1"), "UTF-8");
                ordercode = new String(ordercode.getBytes("ISO-8859-1"), "UTF-8");
                sql = "update mainmenu";
                sql += " set menu_name='" + itemname + "',";
                sql += "menu_uri='" + itemuri + "',";
                sql += "order_code=" + ordercode;
                sql += " where menu_id=" + itemid;
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                stmt.execute(sql);
                TopMenuManager.resetMenus();
                this.msg = "修改成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public boolean deleteMenuItem(String[] itemids) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = "";
        int i = 0;
        if (itemids.length>0) {
            try {
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                conn.setAutoCommit(false);
                for(i=0;i<itemids.length;i++){
                    itemids[i] = new String(itemids[i].getBytes("ISO-8859-1"), "UTF-8");
                sql = "delete from mainmenu where parent_id=" + itemids[i];
                sql += " or menu_id=" + itemids[i];
                stmt.execute(sql);
                }
                conn.commit();
                conn.setAutoCommit(true);
                TopMenuManager.resetMenus();
                this.msg = "删除成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                try{
                conn.rollback();
                }catch(Exception connre){
                    
                }
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }
}
